CS 5010: Problem Set 10

Out: Monday, November 17, 2014

Due: Monday, December 1, 2014 at 600pm local time.

THIS IS THE LAST PROBLEM SET. CODEWALKS FOR THIS PROBLEM SET WILL BE DECEMBER 2-5. THIS WILL BE YOUR LAST OBLIGATION IN THIS COURSE.


The goal of this problem set is to give you practice with stateful classes, and pulling and pushing information. It is also intended to give you practice with reusing code from previous problem sets.

Please restrict yourself to the language features discussed in class. You may use state. You may not use inheritance.

Otherwise, the deliverables and instructions for this problem set are the same as for Problem Set 09.


  1. First, re-implement your solution to problem set 09 using stateful objects. The specifications are exactly the same, except that you must now implement the following interfaces:
    (define StatefulWorld<%>
      (interface ()
    
        ;; -> Void
        ;; EFFECT: updates this World StatefulWorld<%> to the state that it should be in after
        ;; a tick.
        on-tick                             
    
        ;; Integer Integer MouseEvent -> Void
        ;; EFFECT: updates this World<%> StatefulWorld<%> to the state that it should be in
        ;; after the given MouseEvent
        on-mouse
    
        ;; KeyEvent -> Void
        ;; EFFECT: updates this World<%> StatefulWorld<%> to the state that it should be in
        ;; after the given KeyEvent
        on-key
    
        ;; -> Scene
        ;; Returns a Scene depicting this world StatefulWorld<%>
        ;; on it.
        on-draw 
        
        ;; -> Integer
        ;; RETURN: the x and y coordinates of the target
        target-x
        target-y
    
        ;; -> Boolean
        ;; Is the target selected?
        target-selected?
    
        ;; -> ListOfStatefulToy<%>
        get-toys
    
    ))
    
    ;; 
    (define StatefulToy<%> 
      (interface ()
    
        ;; -> Void
        ;; EFFECT: updates this toy StatefulToy<%> to the state it should be in after a
        ;; tick. 
        on-tick                             
    
        ;; Scene -> Scene
        ;; Returns a Scene like the given one, but with this toy StatefulToy<%> drawn
        ;; on it.
        add-to-scene
    
        ;; -> Int
        toy-x
        toy-y
    
        ;; -> ColorString
        ;; returns the current color of this toy StatefulToy<%>
        toy-color
    
        ))
    
    
        
    

    You must provide the same functions and classes as in ps09, except that World%, SquareToy%, and CircleToy% must implement the interfaces above.

    Turn in your solution as a file named "toys.rkt"

  2. Your boss at the toy factory has been reading about Cubelets, which are square blocks that stick together. He asks you to produce a new system similar to the one you just built, with the following new specifications:

    1. The new device will no longer have any circle toys.
    2. The square toys no longer move on their own. Instead, they move by smooth dragging, like the target.
    3. If one square is dragged so that it overlaps another one, the two squares immediately become connected. We refer to them as buddies.
    4. Squares may also move if they are buddies with another toy. Squares become buddies when they overlap while one of those squares is moving. Once two squares are buddies they stay that way forever.
    5. Two squares overlap if they intersect at any point. For this purpose, the edges of the square are considered part of the square.
    6. The relationship of overlapping is not transitive. If A is buddies with B, and A is dragged so that B (not A) intersects with C:

      • then B and C are overlapping,
      • but that doesn't make A and C overlapping.
    7. Buddies travel together. If A and B are buddies, and A is dragged in some direction, then B moves the same way.
    8. The relationship of being a buddy is symmetric and but not transitive: if A is a buddy of B, then B becomes a buddy of A. If A is a buddy of B and B is a buddy of C, that does not mean that A is also a buddy of C, although it may be.
    9. A square moves only when it is dragged or when one of its buddies is dragged. If A is a buddy of B and B is a buddy of C, and A and C are not buddies, then:

      • If A is dragged, B moves with it, but not C, since C is not a buddy of A.
      • If B is dragged, A and C move with it, since they are both buddies of B.
      • If C is dragged, B moves with it, but not A, since A is not a buddy of C.
    10. The target is a black outline circle of radius 10. When selected for dragging, it turns into an orange outline.
    11. A square is created by typing "s", as before. When a square is created, it initially has no buddies, even if it already overlaps another square. However, as soon as it or the other square is dragged moves, buddies are created in the normal way.
    12. The squares now have side 30 and are displayed in outline. Squares are normally green, but when a square is selected, both it and its buddies are displayed in red.
    13. The StatefulToy<%> interface now has an additional operation, toy-selected?
    Examples:
    1. New square B is created and is not initially overlapping with existing square A. A is selected and dragged into B. B changes color to red and starts moving with A (ie, A and B become buddies) as soon as their edges touch.
    2. New square B is created and is initially overlapping with existing square A. A and B are not buddies. A is selected but not dragged. Only A turns red because A and B are not buddies. B is selected but not dragged. Only B turns red. A is selected and dragged. A and B become buddies.
    3. New square C is created and is initially overlapping with existing square B. Another existing square A is buddies with B. A is selected and dragged so that B moves. At that moment, B becomes buddies with C but C does not move and C does not become red. A is unselected and C is selected and dragged. C and B turn red and move together. C is unselected and B is selected and dragged. A, B, and C all turn red and move together.

    There are several places where information must be disseminated in this problem, either by pushing or pulling. Be prepared to identify these and to discuss your design decisions about each of them.

    You must provide the same functions and classes as in the preceding question, except that the specifications for make-world and run are modified as follows following classes, functions, and interfaces. The differences are as follows:

    1. The StatefulWorld<%> interface now has an additional operation, target-color
    2. The StatefulToy<%> interface now has additional operations, on-mouse and toy-selected?
    3. The StatefulToy<%> interface no longer has an on-tick operation.
    4. make-world, run, and make-square-toy no longer have a speed argument
    5. the class and constructor function for circle are no longer needed

    Classes:
    
    World%     -- a class that satisfies the StatefulWorld<%> interface (shown below).
    SquareToy% -- a class that satisfies the StatefulToy<%> interface
    CircleToy%
    
    Functions:
    
    make-world : -> World%
    GIVEN: no arguments
    RETURNS: A World% with no squares.
    
    run : PosNum -> World%
    GIVEN: a frame rate (in seconds/tick)
    EFFECT: creates and runs a world that runs at the given rate.
    RETURNS: the final world.
    
    make-square-toy : PosInt PosInt -> SquareToy%
    GIVEN: an x and a y position
    RETURNS: an object representing a square toy at the given position
    
    make-circle-toy : PosInt PosInt -> CircleToy%
    
    Interfaces:
    
    (define StatefulWorld<%>
      (interface ()
    
        ;; -> Void
        ;; EFFECT: updates this StatefulWorld<%> to the 
        ;;         state that it should be in after a tick.
        on-tick                             
    
        ;; Integer Integer MouseEvent -> Void
        ;; EFFECT: updates this StatefulWorld<%> to the 
        ;;         state that it should be in after the given MouseEvent
        on-mouse
    
        ;; KeyEvent -> Void
        ;; EFFECT: updates this StatefulWorld<%> to the 
        ;;         state that it should be in after the given KeyEvent
        on-key
    
        ;; -> Scene
        ;; Returns a Scene depicting this StatefulWorld<%> on it.
        on-draw 
        
        ;; -> Integer
        ;; RETURN: the x and y coordinates of the target
        target-x
        target-y
    
        ;; -> Boolean
        ;; Is the target selected?
        target-selected?
    
        ;; -> ColorString
        ;; color of the target
        target-color
        
        ;; -> ListOfStatefulToy<%>
        get-toys
    
    ))
    
    ;; 
    (define StatefulToy<%> 
      (interface ()
        
        on-tick
    
        ;; Integer Integer MouseEvent -> Void
        ;; EFFECT: updates this StatefulToy<%> to the 
        ;;         state that it should be in after the given MouseEvent
        on-mouse
    
        ;; Scene -> Scene
        ;; Returns a Scene like the given one, but with this  
        ;; StatefulToy<%> drawn on it.
        add-to-scene
    
        ;; -> Int
        toy-x
        toy-y
    
        ;; -> ColorString
        ;; returns the current color of this StatefulToy<%>
        toy-color
    
         Boolean
        ;; Is this StatefulToy<%> selected?
        toy-selected?
    
        ))
    

    Turn in your solution as a file named "buddies.rkt".

    For what it's worth, my solution was 562 lines exclusive of examples and tests. I debugged my solution using the testing techniques we talked about in the lessons. I wrote 135 lines of tests as I did this. I uncovered a few places where I had failed to correctly convert from functional to imperative form. For those, it was very helpful to have the original formulation using new Whatever% at hand in the comments, as I did in the slides. I also found one place where I had failed to maintain a key invariant. It would have been far more difficult to find that if I I had done the "usual" thing and simply inserted print statements and the like. --Prof. Wand

Qualification Files are at ps10-toys-qualification.rkt and ps10-buddies-qualification.rkt.


Last modified: Sat Nov 29 07:55:16 Eastern Standard Time 2014